package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.UserInfo;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;

import org.bining.footstone.db.DbManager;
import org.bining.footstone.utils.SharedUtils;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.widget.DrawableEditText;

import butterknife.BindView;

/**
 * 意见反馈编辑
 */
public class UserOpinionActivity extends ActionbarActivity {

    @BindView(R.id.et_input)
    DrawableEditText etInput;
    @BindView(R.id.tv_remain_num)
    TextView tvRemainNum;

    private int maxNum;

    private String title = "意见反馈";
    private String data;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_user_opinion;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        if (bundle == null) {
            title = getIntent().getStringExtra("p0");
            data = getIntent().getStringExtra("p1");
        } else {
            title = bundle.getString("p0", title);
            data = bundle.getString("p0", null);
        }

        setTitle(title);
        etInput.setText("");

        switch (title) {
            case "意见反馈":
                etInput.setHint("你提出的，我们都认真倾听~~~");

                maxNum = 300;
                addMenuTextItme(R.string.send, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!isCanClick(v)) return;
                        String opinion = etInput.getText().toString();
                        if (TextUtils.isEmpty(opinion)) {
                            ToastUtils.show(mActivity, "请输入反馈内容后再提交");
                            return;
                        }

                        showProgress();
                        HttpUtils.addFeedback(opinion, new HttpCallback<Object>() {
                            @Override
                            public void onSuccess(Object o) {
                                ToastUtils.show(mActivity, "发送成功！");
                                exit();
                            }

                            @Override
                            public void onError(String strMsg) {
                                super.onError(strMsg);
                                hideProgress();
                                ToastUtils.show(mActivity, strMsg);
                            }
                        });
                    }
                });
                break;
            case "个人简介":
                maxNum = 50;

                if (!TextUtils.isEmpty(data)) {
                    etInput.setText(data);
                    etInput.setSelection(etInput.length());
                }

                addMenuTextItme(R.string.save, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!isCanClick(v)) return;
                        updateUserData();
                    }
                });
                break;
        }

        tvRemainNum.setText("0/" + maxNum);
//        etInput.setMaxEms(maxNum);
        etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxNum)}); //最大输入长度
        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvRemainNum.setText(s.length() + "/" + maxNum);
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("p0", title);
        outState.putString("p1", data);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public String getStatisticsIdentity() {
        return title;
    }

    private void updateUserData() {
        String opinion = etInput.getText().toString();
        HttpUtils.updateUserData(new String[]{"brief_intro"}, new Object[]{opinion}, new HttpCallback<String>() {

            @Override
            public void onStart() {
                super.onStart();
                showProgress();
            }

            @Override
            public void onSuccess(String s) {
                String openID = SharedUtils.getString(Constants.USEROPENID, "");
                UserInfo userInfo = DbManager.getInstance().queryById(openID, UserInfo.class);
                if (userInfo != null) {
                    userInfo.brief_intro = etInput.getText().toString().trim();
                    DbManager.getInstance().update(userInfo);
                    // DbManager.getInstance().update(userInfo, new ColumnsValue(new String[]{""}, new String[]{etName.getText().toString()}), ConflictAlgorithm.None)
                }
                exit();
            }

            @Override
            public void onError(String strMsg) {
                super.onError(strMsg);
                ToastUtils.show(mContext, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                hideProgress();
            }
        });
    }
}
